#include "app_handle.h"
#include "tgmath.h"
#include "tal_memory.h"
#include "app_light_tools.h"
#include "app_pwm.h"
#include "tal_log.h"
#include "ty_scene.h"
STATIC light_ctrl_handle_t sg_light_ctrl_handle;

extern TIMER_ID etimer_light_shade;
extern TIMER_ID etimer_save_data_delay;

const static double forward_pow[] = {
    0.000000, \
    0.000000, 0.000001, 0.000003, 0.000005, 0.000009, 0.000013, 0.000018, 0.000024, 0.000032, 0.000040, 0.000049, 0.000059, 0.000071, 0.000083, 0.000097, 0.000112, 0.000128, 0.000145, 0.000163, 0.000183, \
    0.000204, 0.000226, 0.000249, 0.000273, 0.000299, 0.000326, 0.000354, 0.000383, 0.000414, 0.000446, 0.000480, 0.000514, 0.000550, 0.000588, 0.000627, 0.000667, 0.000708, 0.000751, 0.000795, 0.000840, \
    0.000887, 0.000936, 0.000985, 0.001037, 0.001089, 0.001143, 0.001198, 0.001255, 0.001314, 0.001373, 0.001434, 0.001497, 0.001561, 0.001627, 0.001694, 0.001762, 0.001832, 0.001903, 0.001976, 0.002051, \
    0.002127, 0.002204, 0.002283, 0.002364, 0.002446, 0.002529, 0.002614, 0.002701, 0.002789, 0.002879, 0.002970, 0.003063, 0.003157, 0.003253, 0.003351, 0.003450, 0.003550, 0.003653, 0.003756, 0.003862, \
    0.003969, 0.004077, 0.004188, 0.004299, 0.004413, 0.004528, 0.004645, 0.004763, 0.004883, 0.005004, 0.005127, 0.005252, 0.005379, 0.005507, 0.005636, 0.005768, 0.005901, 0.006035, 0.006172, 0.006310, \
    0.006449, 0.006591, 0.006734, 0.006878, 0.007025, 0.007173, 0.007322, 0.007474, 0.007627, 0.007782, 0.007938, 0.008096, 0.008256, 0.008418, 0.008581, 0.008746, 0.008913, 0.009081, 0.009251, 0.009423, \
    0.009597, 0.009772, 0.009949, 0.010128, 0.010309, 0.010491, 0.010675, 0.010861, 0.011048, 0.011238, 0.011429, 0.011622, 0.011816, 0.012012, 0.012211, 0.012410, 0.012612, 0.012815, 0.013021, 0.013228, \
    0.013436, 0.013647, 0.013859, 0.014073, 0.014289, 0.014507, 0.014726, 0.014948, 0.015171, 0.015396, 0.015622, 0.015851, 0.016081, 0.016313, 0.016547, 0.016783, 0.017021, 0.017260, 0.017501, 0.017745, \
    0.017989, 0.018236, 0.018485, 0.018735, 0.018987, 0.019241, 0.019497, 0.019755, 0.020015, 0.020276, 0.020540, 0.020805, 0.021072, 0.021341, 0.021611, 0.021884, 0.022159, 0.022435, 0.022713, 0.022993, \
    0.023275, 0.023559, 0.023845, 0.024132, 0.024422, 0.024713, 0.025006, 0.025302, 0.025599, 0.025898, 0.026198, 0.026501, 0.026806, 0.027112, 0.027421, 0.027731, 0.028043, 0.028357, 0.028673, 0.028991, \
    0.029311, 0.029633, 0.029957, 0.030282, 0.030610, 0.030939, 0.031270, 0.031604, 0.031939, 0.032276, 0.032615, 0.032956, 0.033299, 0.033644, 0.033991, 0.034340, 0.034691, 0.035043, 0.035398, 0.035754, \
    0.036113, 0.036473, 0.036836, 0.037200, 0.037567, 0.037935, 0.038305, 0.038677, 0.039052, 0.039428, 0.039806, 0.040186, 0.040568, 0.040952, 0.041338, 0.041726, 0.042116, 0.042508, 0.042902, 0.043298, \
    0.043696, 0.044095, 0.044497, 0.044901, 0.045307, 0.045715, 0.046125, 0.046536, 0.046950, 0.047366, 0.047784, 0.048204, 0.048626, 0.049049, 0.049475, 0.049903, 0.050333, 0.050765, 0.051199, 0.051635, \
    0.052073, 0.052513, 0.052954, 0.053398, 0.053844, 0.054292, 0.054743, 0.055195, 0.055649, 0.056105, 0.056563, 0.057023, 0.057485, 0.057950, 0.058416, 0.058884, 0.059355, 0.059827, 0.060302, 0.060778, \
    0.061257, 0.061737, 0.062220, 0.062705, 0.063192, 0.063680, 0.064171, 0.064664, 0.065159, 0.065656, 0.066155, 0.066656, 0.067160, 0.067665, 0.068172, 0.068682, 0.069193, 0.069707, 0.070223, 0.070740, \
    0.071260, 0.071782, 0.072306, 0.072832, 0.073360, 0.073890, 0.074423, 0.074957, 0.075493, 0.076032, 0.076573, 0.077115, 0.077660, 0.078207, 0.078756, 0.079307, 0.079860, 0.080415, 0.080973, 0.081532, \
    0.082094, 0.082658, 0.083223, 0.083791, 0.084361, 0.084933, 0.085508, 0.086084, 0.086662, 0.087243, 0.087826, 0.088410, 0.088997, 0.089586, 0.090178, 0.090771, 0.091366, 0.091964, 0.092563, 0.093165, \
    0.093769, 0.094375, 0.094983, 0.095594, 0.096206, 0.096821, 0.097437, 0.098056, 0.098677, 0.099300, 0.099925, 0.100553, 0.101182, 0.101814, 0.102448, 0.103084, 0.103722, 0.104362, 0.105004, 0.105649, \
    0.106296, 0.106945, 0.107596, 0.108249, 0.108904, 0.109562, 0.110221, 0.110883, 0.111547, 0.112213, 0.112881, 0.113552, 0.114225, 0.114899, 0.115576, 0.116255, 0.116937, 0.117620, 0.118306, 0.118994, \
    0.119684, 0.120376, 0.121070, 0.121767, 0.122465, 0.123166, 0.123869, 0.124575, 0.125282, 0.125992, 0.126704, 0.127418, 0.128134, 0.128852, 0.129573, 0.130295, 0.131020, 0.131748, 0.132477, 0.133209, \
    0.133942, 0.134678, 0.135416, 0.136157, 0.136899, 0.137644, 0.138391, 0.139140, 0.139891, 0.140645, 0.141401, 0.142159, 0.142919, 0.143681, 0.144446, 0.145213, 0.145982, 0.146753, 0.147527, 0.148302, \
    0.149080, 0.149861, 0.150643, 0.151428, 0.152214, 0.153003, 0.153795, 0.154588, 0.155384, 0.156182, 0.156982, 0.157784, 0.158589, 0.159396, 0.160205, 0.161016, 0.161830, 0.162646, 0.163464, 0.164284, \
    0.165107, 0.165932, 0.166759, 0.167588, 0.168419, 0.169253, 0.170089, 0.170927, 0.171768, 0.172611, 0.173456, 0.174303, 0.175152, 0.176004, 0.176858, 0.177714, 0.178573, 0.179434, 0.180297, 0.181162, \
    0.182030, 0.182899, 0.183772, 0.184646, 0.185522, 0.186401, 0.187282, 0.188166, 0.189052, 0.189939, 0.190830, 0.191722, 0.192617, 0.193514, 0.194413, 0.195315, 0.196219, 0.197125, 0.198033, 0.198944, \
    0.199857, 0.200772, 0.201690, 0.202609, 0.203532, 0.204456, 0.205383, 0.206312, 0.207243, 0.208176, 0.209112, 0.210050, 0.210991, 0.211933, 0.212878, 0.213826, 0.214775, 0.215727, 0.216681, 0.217638, \
    0.218596, 0.219557, 0.220521, 0.221486, 0.222454, 0.223425, 0.224397, 0.225372, 0.226349, 0.227329, 0.228311, 0.229295, 0.230281, 0.231270, 0.232261, 0.233254, 0.234250, 0.235248, 0.236248, 0.237251, \
    0.238256, 0.239263, 0.240272, 0.241284, 0.242298, 0.243315, 0.244334, 0.245355, 0.246378, 0.247404, 0.248432, 0.249463, 0.250495, 0.251531, 0.252568, 0.253608, 0.254650, 0.255694, 0.256741, 0.257790, \
    0.258842, 0.259895, 0.260951, 0.262010, 0.263071, 0.264134, 0.265199, 0.266267, 0.267337, 0.268410, 0.269484, 0.270561, 0.271641, 0.272723, 0.273807, 0.274894, 0.275982, 0.277074, 0.278167, 0.279263, \
    0.280361, 0.281462, 0.282565, 0.283670, 0.284778, 0.285888, 0.287001, 0.288115, 0.289232, 0.290352, 0.291474, 0.292598, 0.293724, 0.294853, 0.295985, 0.297118, 0.298254, 0.299393, 0.300533, 0.301677, \
    0.302822, 0.303970, 0.305120, 0.306273, 0.307428, 0.308585, 0.309745, 0.310907, 0.312071, 0.313238, 0.314407, 0.315579, 0.316753, 0.317929, 0.319108, 0.320289, 0.321472, 0.322658, 0.323846, 0.325037, \
    0.326230, 0.327425, 0.328623, 0.329823, 0.331026, 0.332231, 0.333438, 0.334648, 0.335860, 0.337074, 0.338291, 0.339510, 0.340732, 0.341956, 0.343183, 0.344411, 0.345643, 0.346876, 0.348112, 0.349351, \
    0.350592, 0.351835, 0.353080, 0.354329, 0.355579, 0.356832, 0.358087, 0.359345, 0.360605, 0.361867, 0.363132, 0.364399, 0.365669, 0.366941, 0.368216, 0.369493, 0.370772, 0.372054, 0.373338, 0.374624, \
    0.375913, 0.377205, 0.378498, 0.379795, 0.381093, 0.382394, 0.383698, 0.385004, 0.386312, 0.387623, 0.388936, 0.390252, 0.391570, 0.392890, 0.394213, 0.395538, 0.396866, 0.398196, 0.399529, 0.400864, \
    0.402201, 0.403541, 0.404883, 0.406228, 0.407575, 0.408925, 0.410277, 0.411631, 0.412988, 0.414347, 0.415709, 0.417073, 0.418440, 0.419809, 0.421181, 0.422554, 0.423931, 0.425310, 0.426691, 0.428075, \
    0.429461, 0.430850, 0.432241, 0.433634, 0.435030, 0.436428, 0.437829, 0.439233, 0.440638, 0.442047, 0.443457, 0.444870, 0.446286, 0.447704, 0.449124, 0.450547, 0.451973, 0.453400, 0.454831, 0.456263, \
    0.457699, 0.459136, 0.460576, 0.462019, 0.463464, 0.464912, 0.466362, 0.467814, 0.469269, 0.470726, 0.472186, 0.473648, 0.475113, 0.476580, 0.478050, 0.479522, 0.480997, 0.482474, 0.483953, 0.485435, \
    0.486920, 0.488407, 0.489896, 0.491388, 0.492883, 0.494380, 0.495879, 0.497381, 0.498885, 0.500392, 0.501901, 0.503413, 0.504927, 0.506444, 0.507963, 0.509485, 0.511009, 0.512536, 0.514065, 0.515596, \
    0.517130, 0.518667, 0.520206, 0.521748, 0.523292, 0.524838, 0.526387, 0.527939, 0.529493, 0.531049, 0.532608, 0.534170, 0.535734, 0.537300, 0.538869, 0.540441, 0.542015, 0.543591, 0.545170, 0.546751, \
    0.548335, 0.549922, 0.551511, 0.553102, 0.554696, 0.556293, 0.557892, 0.559493, 0.561097, 0.562703, 0.564312, 0.565924, 0.567538, 0.569154, 0.570773, 0.572395, 0.574019, 0.575645, 0.577275, 0.578906, \
    0.580540, 0.582177, 0.583816, 0.585457, 0.587102, 0.588748, 0.590397, 0.592049, 0.593703, 0.595360, 0.597019, 0.598681, 0.600345, 0.602012, 0.603681, 0.605353, 0.607027, 0.608704, 0.610384, 0.612066, \
    0.613750, 0.615437, 0.617127, 0.618819, 0.620513, 0.622210, 0.623910, 0.625612, 0.627317, 0.629024, 0.630733, 0.632446, 0.634161, 0.635878, 0.637598, 0.639320, 0.641045, 0.642772, 0.644502, 0.646235, \
    0.647970, 0.649708, 0.651448, 0.653191, 0.654936, 0.656683, 0.658434, 0.660187, 0.661942, 0.663700, 0.665460, 0.667223, 0.668989, 0.670757, 0.672528, 0.674301, 0.676077, 0.677855, 0.679636, 0.681419, \
    0.683205, 0.684994, 0.686785, 0.688578, 0.690375, 0.692173, 0.693974, 0.695778, 0.697585, 0.699394, 0.701205, 0.703019, 0.704836, 0.706655, 0.708477, 0.710301, 0.712128, 0.713957, 0.715789, 0.717623, \
    0.719460, 0.721300, 0.723142, 0.724987, 0.726834, 0.728684, 0.730537, 0.732392, 0.734249, 0.736109, 0.737972, 0.739837, 0.741705, 0.743576, 0.745449, 0.747324, 0.749202, 0.751083, 0.752966, 0.754852, \
    0.756741, 0.758632, 0.760525, 0.762421, 0.764320, 0.766221, 0.768125, 0.770032, 0.771941, 0.773852, 0.775766, 0.777683, 0.779602, 0.781524, 0.783449, 0.785376, 0.787306, 0.789238, 0.791173, 0.793110, \
    0.795050, 0.796993, 0.798938, 0.800886, 0.802836, 0.804789, 0.806745, 0.808703, 0.810663, 0.812627, 0.814593, 0.816561, 0.818532, 0.820506, 0.822482, 0.824461, 0.826442, 0.828426, 0.830413, 0.832402, \
    0.834394, 0.836388, 0.838385, 0.840385, 0.842387, 0.844392, 0.846400, 0.848410, 0.850422, 0.852437, 0.854455, 0.856476, 0.858499, 0.860524, 0.862553, 0.864583, 0.866617, 0.868653, 0.870691, 0.872733, \
    0.874777, 0.876823, 0.878872, 0.880924, 0.882978, 0.885035, 0.887095, 0.889157, 0.891222, 0.893289, 0.895359, 0.897431, 0.899507, 0.901584, 0.903665, 0.905748, 0.907834, 0.909922, 0.912013, 0.914106, \
    0.916202, 0.918301, 0.920403, 0.922507, 0.924613, 0.926722, 0.928834, 0.930949, 0.933066, 0.935186, 0.937308, 0.939433, 0.941561, 0.943691, 0.945824, 0.947959, 0.950097, 0.952238, 0.954381, 0.956527, \
    0.958676, 0.960827, 0.962981, 0.965138, 0.967297, 0.969458, 0.971623, 0.973790, 0.975960, 0.978132, 0.980307, 0.982484, 0.984665, 0.986848, 0.989033, 0.991221, 0.993412, 0.995605, 0.997801, 1.000000
};

const static double reversal_pow[] = {
    0.000000, \
    0.043288, 0.059319, 0.071324, 0.081288, 0.089966, 0.097739, 0.104833, 0.111393, 0.117520, 0.123285, 0.128743, 0.133937, 0.138900, 0.143658, 0.148235, 0.152648, 0.156913, 0.161043, 0.165050, 0.168943, \
    0.172732, 0.176423, 0.180024, 0.183541, 0.186978, 0.190341, 0.193635, 0.196862, 0.200028, 0.203134, 0.206184, 0.209181, 0.212128, 0.215026, 0.217878, 0.220686, 0.223451, 0.226176, 0.228863, 0.231512, \
    0.234125, 0.236703, 0.239249, 0.241762, 0.244244, 0.246696, 0.249120, 0.251515, 0.253884, 0.256226, 0.258542, 0.260835, 0.263103, 0.265348, 0.267570, 0.269771, 0.271950, 0.274108, 0.276246, 0.278365, \
    0.280464, 0.282545, 0.284607, 0.286652, 0.288679, 0.290689, 0.292683, 0.294661, 0.296623, 0.298569, 0.300500, 0.302417, 0.304319, 0.306207, 0.308081, 0.309941, 0.311788, 0.313622, 0.315443, 0.317252, \
    0.319049, 0.320833, 0.322606, 0.324367, 0.326116, 0.327854, 0.329582, 0.331298, 0.333004, 0.334700, 0.336385, 0.338061, 0.339726, 0.341381, 0.343027, 0.344664, 0.346291, 0.347910, 0.349519, 0.351119, \
    0.352711, 0.354294, 0.355869, 0.357435, 0.358993, 0.360543, 0.362085, 0.363619, 0.365146, 0.366665, 0.368176, 0.369680, 0.371177, 0.372667, 0.374149, 0.375624, 0.377093, 0.378554, 0.380009, 0.381457, \
    0.382899, 0.384334, 0.385763, 0.387185, 0.388602, 0.390012, 0.391416, 0.392813, 0.394205, 0.395592, 0.396972, 0.398346, 0.399715, 0.401079, 0.402436, 0.403789, 0.405135, 0.406477, 0.407813, 0.409144, \
    0.410470, 0.411791, 0.413106, 0.414417, 0.415723, 0.417023, 0.418319, 0.419610, 0.420897, 0.422178, 0.423455, 0.424728, 0.425996, 0.427259, 0.428518, 0.429772, 0.431022, 0.432268, 0.433510, 0.434747, \
    0.435980, 0.437209, 0.438433, 0.439654, 0.440870, 0.442083, 0.443291, 0.444496, 0.445697, 0.446894, 0.448087, 0.449276, 0.450461, 0.451643, 0.452821, 0.453995, 0.455166, 0.456333, 0.457496, 0.458656, \
    0.459813, 0.460966, 0.462115, 0.463262, 0.464404, 0.465544, 0.466680, 0.467812, 0.468942, 0.470068, 0.471191, 0.472311, 0.473427, 0.474541, 0.475651, 0.476758, 0.477862, 0.478963, 0.480061, 0.481157, \
    0.482249, 0.483338, 0.484424, 0.485507, 0.486587, 0.487665, 0.488739, 0.489811, 0.490880, 0.491946, 0.493010, 0.494071, 0.495129, 0.496184, 0.497236, 0.498286, 0.499334, 0.500378, 0.501420, 0.502460, \
    0.503497, 0.504531, 0.505563, 0.506592, 0.507619, 0.508643, 0.509665, 0.510684, 0.511701, 0.512715, 0.513727, 0.514737, 0.515744, 0.516749, 0.517752, 0.518752, 0.519750, 0.520746, 0.521739, 0.522730, \
    0.523719, 0.524706, 0.525690, 0.526673, 0.527653, 0.528631, 0.529606, 0.530580, 0.531551, 0.532521, 0.533488, 0.534453, 0.535416, 0.536377, 0.537336, 0.538292, 0.539247, 0.540200, 0.541151, 0.542099, \
    0.543046, 0.543991, 0.544934, 0.545874, 0.546813, 0.547750, 0.548685, 0.549618, 0.550550, 0.551479, 0.552406, 0.553332, 0.554256, 0.555178, 0.556098, 0.557016, 0.557933, 0.558847, 0.559760, 0.560671, \
    0.561580, 0.562488, 0.563394, 0.564298, 0.565200, 0.566101, 0.567000, 0.567897, 0.568792, 0.569686, 0.570578, 0.571468, 0.572357, 0.573244, 0.574130, 0.575014, 0.575896, 0.576776, 0.577655, 0.578533, \
    0.579408, 0.580283, 0.581155, 0.582026, 0.582896, 0.583764, 0.584630, 0.585495, 0.586358, 0.587220, 0.588080, 0.588939, 0.589796, 0.590652, 0.591506, 0.592359, 0.593210, 0.594060, 0.594909, 0.595756, \
    0.596601, 0.597445, 0.598288, 0.599129, 0.599969, 0.600807, 0.601644, 0.602480, 0.603314, 0.604147, 0.604979, 0.605809, 0.606637, 0.607465, 0.608291, 0.609116, 0.609939, 0.610761, 0.611582, 0.612401, \
    0.613219, 0.614036, 0.614851, 0.615665, 0.616478, 0.617290, 0.618100, 0.618909, 0.619717, 0.620523, 0.621329, 0.622133, 0.622935, 0.623737, 0.624537, 0.625336, 0.626134, 0.626931, 0.627726, 0.628520, \
    0.629313, 0.630105, 0.630896, 0.631685, 0.632473, 0.633260, 0.634046, 0.634831, 0.635615, 0.636397, 0.637178, 0.637958, 0.638737, 0.639515, 0.640292, 0.641067, 0.641842, 0.642615, 0.643387, 0.644158, \
    0.644928, 0.645697, 0.646465, 0.647232, 0.647997, 0.648762, 0.649525, 0.650287, 0.651049, 0.651809, 0.652568, 0.653326, 0.654083, 0.654839, 0.655594, 0.656348, 0.657101, 0.657853, 0.658604, 0.659353, \
    0.660102, 0.660850, 0.661597, 0.662342, 0.663087, 0.663831, 0.664573, 0.665315, 0.666056, 0.666796, 0.667534, 0.668272, 0.669009, 0.669745, 0.670479, 0.671213, 0.671946, 0.672678, 0.673409, 0.674139, \
    0.674868, 0.675597, 0.676324, 0.677050, 0.677776, 0.678500, 0.679223, 0.679946, 0.680668, 0.681388, 0.682108, 0.682827, 0.683545, 0.684262, 0.684978, 0.685694, 0.686408, 0.687122, 0.687834, 0.688546, \
    0.689257, 0.689967, 0.690676, 0.691384, 0.692092, 0.692798, 0.693504, 0.694209, 0.694913, 0.695616, 0.696318, 0.697019, 0.697720, 0.698419, 0.699118, 0.699816, 0.700513, 0.701210, 0.701905, 0.702600, \
    0.703294, 0.703987, 0.704679, 0.705370, 0.706061, 0.706751, 0.707440, 0.708128, 0.708815, 0.709502, 0.710188, 0.710873, 0.711557, 0.712240, 0.712923, 0.713605, 0.714286, 0.714966, 0.715646, 0.716324, \
    0.717002, 0.717679, 0.718356, 0.719031, 0.719706, 0.720380, 0.721054, 0.721726, 0.722398, 0.723069, 0.723740, 0.724410, 0.725078, 0.725747, 0.726414, 0.727081, 0.727747, 0.728412, 0.729076, 0.729740, \
    0.730403, 0.731065, 0.731727, 0.732388, 0.733048, 0.733708, 0.734366, 0.735024, 0.735682, 0.736338, 0.736994, 0.737649, 0.738304, 0.738958, 0.739611, 0.740263, 0.740915, 0.741566, 0.742217, 0.742866, \
    0.743515, 0.744164, 0.744811, 0.745458, 0.746105, 0.746750, 0.747395, 0.748039, 0.748683, 0.749326, 0.749968, 0.750610, 0.751251, 0.751891, 0.752531, 0.753170, 0.753809, 0.754446, 0.755083, 0.755720, \
    0.756356, 0.756991, 0.757625, 0.758259, 0.758892, 0.759525, 0.760157, 0.760788, 0.761419, 0.762049, 0.762679, 0.763308, 0.763936, 0.764563, 0.765190, 0.765817, 0.766443, 0.767068, 0.767692, 0.768316, \
    0.768940, 0.769562, 0.770184, 0.770806, 0.771427, 0.772047, 0.772667, 0.773286, 0.773905, 0.774522, 0.775140, 0.775757, 0.776373, 0.776988, 0.777603, 0.778218, 0.778832, 0.779445, 0.780058, 0.780670, \
    0.781281, 0.781892, 0.782502, 0.783112, 0.783721, 0.784330, 0.784938, 0.785546, 0.786153, 0.786759, 0.787365, 0.787970, 0.788575, 0.789179, 0.789783, 0.790386, 0.790988, 0.791590, 0.792192, 0.792793, \
    0.793393, 0.793993, 0.794592, 0.795191, 0.795789, 0.796387, 0.796984, 0.797580, 0.798176, 0.798772, 0.799367, 0.799961, 0.800555, 0.801148, 0.801741, 0.802333, 0.802925, 0.803516, 0.804107, 0.804697, \
    0.805287, 0.805876, 0.806465, 0.807053, 0.807641, 0.808228, 0.808814, 0.809401, 0.809986, 0.810571, 0.811156, 0.811740, 0.812323, 0.812906, 0.813489, 0.814071, 0.814653, 0.815234, 0.815814, 0.816394, \
    0.816974, 0.817553, 0.818132, 0.818710, 0.819287, 0.819864, 0.820441, 0.821017, 0.821593, 0.822168, 0.822743, 0.823317, 0.823891, 0.824464, 0.825037, 0.825609, 0.826181, 0.826752, 0.827323, 0.827894, \
    0.828464, 0.829033, 0.829602, 0.830171, 0.830739, 0.831306, 0.831873, 0.832440, 0.833006, 0.833572, 0.834137, 0.834702, 0.835266, 0.835830, 0.836394, 0.836957, 0.837519, 0.838081, 0.838643, 0.839204, \
    0.839765, 0.840325, 0.840885, 0.841445, 0.842003, 0.842562, 0.843120, 0.843678, 0.844235, 0.844792, 0.845348, 0.845904, 0.846459, 0.847014, 0.847569, 0.848123, 0.848676, 0.849230, 0.849783, 0.850335, \
    0.850887, 0.851438, 0.851989, 0.852540, 0.853090, 0.853640, 0.854190, 0.854739, 0.855287, 0.855835, 0.856383, 0.856930, 0.857477, 0.858024, 0.858570, 0.859115, 0.859660, 0.860205, 0.860749, 0.861293, \
    0.861837, 0.862380, 0.862923, 0.863465, 0.864007, 0.864549, 0.865090, 0.865630, 0.866171, 0.866710, 0.867250, 0.867789, 0.868328, 0.868866, 0.869404, 0.869941, 0.870478, 0.871015, 0.871551, 0.872087, \
    0.872623, 0.873158, 0.873692, 0.874227, 0.874761, 0.875294, 0.875827, 0.876360, 0.876892, 0.877424, 0.877956, 0.878487, 0.879018, 0.879548, 0.880078, 0.880608, 0.881137, 0.881666, 0.882195, 0.882723, \
    0.883251, 0.883778, 0.884305, 0.884832, 0.885358, 0.885884, 0.886409, 0.886934, 0.887459, 0.887983, 0.888507, 0.889031, 0.889554, 0.890077, 0.890600, 0.891122, 0.891644, 0.892165, 0.892686, 0.893207, \
    0.893727, 0.894247, 0.894767, 0.895286, 0.895805, 0.896324, 0.896842, 0.897359, 0.897877, 0.898394, 0.898911, 0.899427, 0.899943, 0.900459, 0.900974, 0.901489, 0.902004, 0.902518, 0.903032, 0.903545, \
    0.904059, 0.904571, 0.905084, 0.905596, 0.906108, 0.906619, 0.907131, 0.907641, 0.908152, 0.908662, 0.909172, 0.909681, 0.910190, 0.910699, 0.911207, 0.911715, 0.912223, 0.912730, 0.913237, 0.913744, \
    0.914250, 0.914756, 0.915262, 0.915767, 0.916272, 0.916777, 0.917281, 0.917785, 0.918289, 0.918792, 0.919295, 0.919798, 0.920300, 0.920802, 0.921304, 0.921805, 0.922306, 0.922807, 0.923307, 0.923808, \
    0.924307, 0.924807, 0.925306, 0.925805, 0.926303, 0.926801, 0.927299, 0.927796, 0.928294, 0.928790, 0.929287, 0.929783, 0.930279, 0.930775, 0.931270, 0.931765, 0.932259, 0.932754, 0.933248, 0.933741, \
    0.934235, 0.934728, 0.935220, 0.935713, 0.936205, 0.936697, 0.937188, 0.937679, 0.938170, 0.938661, 0.939151, 0.939641, 0.940131, 0.940620, 0.941109, 0.941598, 0.942086, 0.942575, 0.943062, 0.943550, \
    0.944037, 0.944524, 0.945011, 0.945497, 0.945983, 0.946469, 0.946954, 0.947439, 0.947924, 0.948409, 0.948893, 0.949377, 0.949860, 0.950344, 0.950827, 0.951309, 0.951792, 0.952274, 0.952756, 0.953238, \
    0.953719, 0.954200, 0.954681, 0.955161, 0.955641, 0.956121, 0.956600, 0.957080, 0.957559, 0.958037, 0.958516, 0.958994, 0.959472, 0.959949, 0.960426, 0.960903, 0.961380, 0.961857, 0.962333, 0.962809, \
    0.963284, 0.963759, 0.964234, 0.964709, 0.965183, 0.965658, 0.966132, 0.966605, 0.967078, 0.967551, 0.968024, 0.968497, 0.968969, 0.969441, 0.969912, 0.970384, 0.970855, 0.971326, 0.971796, 0.972267, \
    0.972737, 0.973206, 0.973676, 0.974145, 0.974614, 0.975083, 0.975551, 0.976019, 0.976487, 0.976955, 0.977422, 0.977889, 0.978356, 0.978822, 0.979288, 0.979754, 0.980220, 0.980686, 0.981151, 0.981616, \
    0.982080, 0.982545, 0.983009, 0.983473, 0.983936, 0.984400, 0.984863, 0.985325, 0.985788, 0.986250, 0.986712, 0.987174, 0.987636, 0.988097, 0.988558, 0.989019, 0.989479, 0.989939, 0.990399, 0.990859, \
    0.991318, 0.991778, 0.992237, 0.992695, 0.993154, 0.993612, 0.994070, 0.994527, 0.994985, 0.995442, 0.995899, 0.996356, 0.996812, 0.997268, 0.997724, 0.998180, 0.998635, 0.999090, 0.999545, 1.000000
};

STATIC VOID_T app_handle_data_reversal(LIGHT_DATA_T *data)
{
    double sum = data->red + data->green + data->blue + data->white + data->warm;
    UINT_T aisle = (data->red == 0) ? 0 : 1;
    aisle += (data->green == 0) ? 0 : 1;
    aisle += (data->blue == 0) ? 0 : 1;
    aisle += (data->white == 0) ? 0 : 1;
    aisle += (data->warm == 0) ? 0 : 1;
    if (aisle != 0)
    {
        UINT_T sub = sum / aisle;
        if (sub > CTRL_CAL_VALUE_RANGE) sub = CTRL_CAL_VALUE_RANGE;
        double dSum = CTRL_CAL_VALUE_RANGE * aisle * reversal_pow[sub];
        data->red = (UINT16_T)(data->red * dSum / sum);
        data->green = (UINT16_T)(data->green * dSum / sum);
        data->blue = (UINT16_T)(data->blue * dSum / sum);
        data->white = (UINT16_T)(data->white * dSum / sum);
        data->warm = (UINT16_T)(data->warm * dSum / sum);
    }
}

STATIC VOID_T app_handle_gradual(VOID_T)
{
    sg_light_ctrl_handle.steps_cnt++;
    USHORT_T cnt = sg_light_ctrl_handle.steps_cnt & GRADUAL_CNT_BIT;
    USHORT_T max = sg_light_ctrl_handle.steps_max & GRADUAL_CNT_BIT;
    if (cnt < max)
    {
        sg_light_ctrl_handle.current_val.red = app_light_tools_GetMiddleValue(sg_light_ctrl_handle.original_val.red, sg_light_ctrl_handle.target_val.red, cnt, max);
        sg_light_ctrl_handle.current_val.green = app_light_tools_GetMiddleValue(sg_light_ctrl_handle.original_val.green, sg_light_ctrl_handle.target_val.green, cnt, max);
        sg_light_ctrl_handle.current_val.blue = app_light_tools_GetMiddleValue(sg_light_ctrl_handle.original_val.blue, sg_light_ctrl_handle.target_val.blue, cnt, max);
        sg_light_ctrl_handle.current_val.white = app_light_tools_GetMiddleValue(sg_light_ctrl_handle.original_val.white, sg_light_ctrl_handle.target_val.white, cnt, max);
        sg_light_ctrl_handle.current_val.warm = app_light_tools_GetMiddleValue(sg_light_ctrl_handle.original_val.warm, sg_light_ctrl_handle.target_val.warm, cnt, max);
    }else {
        sg_light_ctrl_handle.current_val.red = sg_light_ctrl_handle.target_val.red;
        sg_light_ctrl_handle.current_val.green = sg_light_ctrl_handle.target_val.green;
        sg_light_ctrl_handle.current_val.blue = sg_light_ctrl_handle.target_val.blue;
        sg_light_ctrl_handle.current_val.white = sg_light_ctrl_handle.target_val.white;
        sg_light_ctrl_handle.current_val.warm = sg_light_ctrl_handle.target_val.warm;
        if (sg_light_ctrl_handle.steps_max & TASK_LOOP_FLAG)
        {
            sg_light_ctrl_handle.steps_cnt = TASK_LOOP_FLAG | RESET_GIVE_FLAG;
        } else {
            sg_light_ctrl_handle.steps_cnt = 0;
            tal_sw_timer_stop(etimer_light_shade);
        }
    }
    
    if (sg_light_ctrl_handle.shade_param == SHADE_PARAM_BRIGHT)
    {
        double sum = sg_light_ctrl_handle.current_val.red + \
                    sg_light_ctrl_handle.current_val.green + \
                    sg_light_ctrl_handle.current_val.blue + \
                    sg_light_ctrl_handle.current_val.white + \
                    sg_light_ctrl_handle.current_val.warm;
        UINT_T aisle = (sg_light_ctrl_handle.current_val.red == 0) ? 0 : 1;
        aisle += (sg_light_ctrl_handle.current_val.green == 0) ? 0 : 1;
        aisle += (sg_light_ctrl_handle.current_val.blue == 0) ? 0 : 1;
        aisle += (sg_light_ctrl_handle.current_val.white == 0) ? 0 : 1;
        aisle += (sg_light_ctrl_handle.current_val.warm == 0) ? 0 : 1;
        if (aisle != 0)
        {
            UINT_T sub = sum / aisle;
            if (sub > CTRL_CAL_VALUE_RANGE) sub = CTRL_CAL_VALUE_RANGE;
            double dSum = CTRL_CAL_VALUE_RANGE * aisle * forward_pow[sub];
            sg_light_ctrl_handle.current_val.red = (UINT16_T)(sg_light_ctrl_handle.current_val.red * dSum / sum);
            sg_light_ctrl_handle.current_val.green = (UINT16_T)(sg_light_ctrl_handle.current_val.green * dSum / sum);
            sg_light_ctrl_handle.current_val.blue = (UINT16_T)(sg_light_ctrl_handle.current_val.blue * dSum / sum);
            sg_light_ctrl_handle.current_val.white = (UINT16_T)(sg_light_ctrl_handle.current_val.white * dSum / sum);
            sg_light_ctrl_handle.current_val.warm = (UINT16_T)(sg_light_ctrl_handle.current_val.warm * dSum / sum);
        }
    }
    app_pwm_cw_set(sg_light_ctrl_handle.current_val.white, sg_light_ctrl_handle.current_val.warm);
}

VOID_T app_light_ctrl_running_cb(TIMER_ID timer_id, VOID_T *arg)
{
    static UINT8_T onLightFlag = 0;
    tal_sw_timer_start(etimer_light_shade, HANLD_CYCLE_MS, TAL_TIMER_ONCE);
    if (sg_light_ctrl_handle.steps_cnt & TASK_LOOP_FLAG)
    {
        if (sg_light_ctrl_handle.steps_cnt & RESET_GIVE_FLAG)
        {
            tal_system_memcpy((UINT8_T *)&sg_light_ctrl_handle.original_val, (const UINT8_T *)&sg_light_ctrl_handle.current_val, sizeof(LIGHT_DATA_T));
            if (app_light_ctrl_nwk_flag_get() == TRUE)
            {
                app_light_ctrl_blink_display(&sg_light_ctrl_handle.target_val);
                sg_light_ctrl_handle.shade_param = app_light_ctrl_shade_param_get();
                app_light_ctrl_remain_time_ms_get(&sg_light_ctrl_handle.steps_max);
                sg_light_ctrl_handle.steps_max = TASK_LOOP_FLAG | (sg_light_ctrl_handle.steps_max * 100 / HANLD_CYCLE_MS);
            } else {
                if (app_light_ctrl_data_switch_get() == TRUE)
                {
                    switch (app_light_ctrl_data_mode_get())
                    {
                        case WHITE_MODE:
                        {
                            sg_light_ctrl_handle.shade_param = app_light_ctrl_shade_param_get();
                            app_light_ctrl_white_target_val(&sg_light_ctrl_handle.target_val);
                            app_light_ctrl_remain_time_ms_get(&sg_light_ctrl_handle.steps_max);
                            sg_light_ctrl_handle.steps_max = sg_light_ctrl_handle.steps_max * 100 / HANLD_CYCLE_MS;
                        }
                            break;

                        case COLOR_MODE:
                        {
                        
                        }
                            break;

                        case SCENE_MODE:
                        {
                            sg_light_ctrl_handle.shade_param = SHADE_PARAM_COLOR;
                            TY_Scene_Get(&sg_light_ctrl_handle.target_val, &sg_light_ctrl_handle.steps_max);
                        }
                            break;

                        case MUSIC_MODE:
                        {
                        
                        }
                            break;

                        default:
                        {
                        
                        }
                            break;
                    }
                    if (onLightFlag == 1)
                    {
                        sg_light_ctrl_handle.shade_param = SHADE_PARAM_BRIGHT;
                        sg_light_ctrl_handle.steps_max &= ~GRADUAL_CNT_BIT;
                        sg_light_ctrl_handle.steps_max |= app_light_ctrl_data_switch_on_gradient() / HANLD_CYCLE_MS;
                        onLightFlag = 0;
                    }
                } else { 
                    sg_light_ctrl_handle.shade_param = SHADE_PARAM_BRIGHT;
                    memset(&sg_light_ctrl_handle.target_val, 0, sizeof(LIGHT_DATA_T));
                    sg_light_ctrl_handle.steps_max &= ~GRADUAL_CNT_BIT;
                    sg_light_ctrl_handle.steps_max |= app_light_ctrl_data_switch_off_gradient() / HANLD_CYCLE_MS;
                    onLightFlag = 1;
                }
            }
            if (sg_light_ctrl_handle.shade_param == SHADE_PARAM_BRIGHT)
            {
                app_handle_data_reversal(&sg_light_ctrl_handle.original_val);
                app_handle_data_reversal(&sg_light_ctrl_handle.target_val);
            }
            sg_light_ctrl_handle.steps_cnt = TASK_LOOP_FLAG;
        }
        app_handle_gradual();
    }
}

OPERATE_RET app_light_ctrl_handle_data_get(LIGHT_DATA_T *cur_data)
{
    if(cur_data == NULL)
    {
        return OPRT_COM_ERROR;
    }
    memcpy(cur_data, &sg_light_ctrl_handle.current_val, sizeof(LIGHT_DATA_T));
    return OPRT_OK;
}

VOID_T app_handle_init(VOID_T)
{
    tal_system_memset(&sg_light_ctrl_handle, 0, SIZEOF(light_ctrl_handle_t));
    app_handle_start(FALSE);
}

VOID_T app_handle_start(UINT8_T save_flag)
{
    sg_light_ctrl_handle.steps_cnt = TASK_LOOP_FLAG | RESET_GIVE_FLAG;
    tal_sw_timer_start(etimer_light_shade, HANLD_CYCLE_MS, TAL_TIMER_ONCE);
    if (save_flag == TRUE)
    {
        tal_sw_timer_start(etimer_save_data_delay, 5000, TAL_TIMER_ONCE);
    }
}

